package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.config.AliPayConfig;
import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.PayFlow;
import cn.itsource.ymcc.domain.PayOrder;
import cn.itsource.ymcc.mapper.PayFlowMapper;
import cn.itsource.ymcc.mapper.PayOrderMapper;
import cn.itsource.ymcc.producer.IPayProducer;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.service.IPayOrderService;
import cn.itsource.ymcc.utils.AssertUtil;
import cn.itsource.ymcc.vo.PayVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zongcj
 * @since 2023-09-25
 */
@Service
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements IPayOrderService {
    @Autowired
    private AliPayConfig aliPayConfig;
    @Autowired
    private PayFlowMapper payFlowMapper;
    @Autowired
    private IPayProducer iPayProducer;

    @Override
    public JsonResult apply(PayVo payVo) {
        String orderNo = payVo.getOrderNo();
        PayOrder payOrder = baseMapper.selectOne(new LambdaQueryWrapper<PayOrder>().eq(PayOrder::getOrderNo, orderNo));
        AssertUtil.isNotNull(payOrder,"非法请求");
        String payHtml = aliPayConfig.pay(payOrder.getSubject(), orderNo, payOrder.getAmount().toString(), payVo.getCallUrl());
        return JsonResult.success().setData(payHtml);
    }

    @Override
    @Transactional
    public void asyncNotify(Map<String, String> parameter) throws ParseException {
        // 修改支付账单状态、保存支付流水记录【支付服务中】
        String orderNo = parameter.get("out_trade_no");
        PayOrder payOrder = baseMapper.selectOne(new LambdaQueryWrapper<PayOrder>().eq(PayOrder::getOrderNo, orderNo));
        payOrder.setPayStatus(RConst.Num.INT_1);
        payOrder.setUpdateTime(new Date());
        baseMapper.updateById(payOrder);

        //2.添加支付流水
        PayFlow payFlow = new PayFlow();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        payFlow.setNotifyTime(sdf.parse(parameter.get("notify_time")));
        payFlow.setSubject(parameter.get("subject"));
        payFlow.setOutTradeNo(orderNo);
        payFlow.setTotalAmount(new BigDecimal(parameter.get("total_amount")));
        payFlow.setTradeStatus(parameter.get("trade_status"));
        payFlow.setPaySuccess(true);
        payFlow.setResultDesc("交易成功");
        payFlowMapper.insert(payFlow);
        //修改订单状态【在订单服务中】
        Map<String,Object> map = new HashMap<>();
        map.put("orderNo",orderNo);
        map.put("courseIds",payOrder.getRelationId());
        map.put("userId",payOrder.getUserId());
        // 添加购买记录
        iPayProducer.updateCourseOrderStatus(map);
    }
}
